[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
m/PATTERN/gio
/PATTERN/gio
Searches a string for a pattern match, and returns
true (1) or false (''). If no string is specified
via the =~ or !~ operator, the $_ string is
searched. (The string specified with =~ need not be
an lvalue--it may be the result of an expression
evaluation, but remember the =~ binds rather
tightly.) See also the section on regular expres-
sions.
If / is the delimiter then the initial 'm' is
optional. With the 'm' you can use any pair of
non-alphanumeric characters as delimiters. This is
particularly useful for matching Unix path names
that contain '/'. If the final delimiter is fol-
lowed by the optional letter 'i', the matching is
done in a case-insensitive manner. PATTERN may con-
tain references to scalar variables, which will be
interpolated (and the pattern recompiled) every time
the pattern search is evaluated. (Note that $) and
$| may not be interpolated because they look like
end-of-string tests.) If you want such a pattern to
be compiled only once, add an "o" after the trailing
delimiter. This avoids expensive run-time recompi-
lations, and is useful when the value you are inter-
polating won't change over the life of the script.
If the PATTERN evaluates to a null string, the most
recent successful regular expression is used
instead.
If used in a context that requires an array value, a
pattern match returns an array consisting of the
subexpressions matched by the parentheses in the
pattern, i.e. ($1, $2, $3...). It does NOT actually
set $1, $2, etc. in this case, nor does it set $+,
$`, $& or $'. If the match fails, a null array is
returned. If the match succeeds, but there were no
parentheses, an array value of (1) is returned.
Examples:
open(tty, '/dev/tty');
<tty> =~ /^y/i && do foo(); # do foo if desired
if (/Version: *([0-9.]*)/) { $version = $1; }
next if m#^/usr/spool/uucp#;
# poor man's grep
$arg = shift;
while (<>) {
print if /$arg/o; # compile only once
}
if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
This last example splits $foo into the first two
words and the remainder of the line, and assigns
those three fields to $F1, $F2 and $Etc. The condi-
tional is true if any variables were assigned, i.e.
if the pattern matched.
The "g" modifier specifies global pattern
matching--that is, matching as many times as possi-
ble within the string. How it behaves depends on
the context. In an array context, it returns a list
of all the substrings matched by all the parentheses
in the regular expression. If there are no
parentheses, it returns a list of all the matched
strings, as if there were parentheses around the
whole pattern. In a scalar context, it iterates
through the string, returning TRUE each time it
matches, and FALSE when it eventually runs out of
matches. (In other words, it remembers where it
left off last time and restarts the search at that
point.) It presumes that you have not modified the
string since the last match. Modifying the string
between matches may result in undefined behavior.
(You can actually get away with in-place modifica-
tions via substr() that do not change the length of
the entire string. In general, however, you should
be using s///g for such modifications.) Examples:
# array context
($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g);
# scalar context
$/ = 1; $* = 1;
while ($paragraph = <>) {
while ($paragraph =~ /[a-z]['")]*[.!?]+['")]*\s/g) {
$sentences++;
}
}
print "$sentences\n";
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson